//
//  HWevaluationshituView.m
//  HWanGame
//
//  Created by 陈道斌 on 2021/9/8.
//  Copyright © 2021 Chendaobin. All rights reserved.
//

#import "HWevaluationshituView.h"
#import "JXPagerView.h"
#import "HWOpenClothingCell.h"

@interface HWevaluationshituView()<JXPagerViewListViewDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);
@property(strong,nonatomic) UITableView *maintablview;
@property(strong,nonatomic) UIImageView *shujuimagvie;
@property(strong,nonatomic) UILabel *zhawushujuLabel;
@property(strong,nonatomic) NSMutableArray *shujuMuArr;
@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;



@end

@implementation HWevaluationshituView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

       //  [self setUI];
      //   [self detailsInterfaceCommentsxiangqing];
        
        [self setUPUI];
        
        
        
    }
    return self;
}



- (void)layoutSubviews {
    [super layoutSubviews];

    self.maintablview.frame = self.bounds;
}

- (void)refreshData {
    
    // [self detailsInterfaceCommentsxiangqing];
    
    [self underRefresh];
    
    
}



- (void)setUPUI {
    

    self.shujuimagvie = [[UIImageView alloc]initWithFrame:CGRectMake((kScreenW-188)/2, 33, 188, 124)];
    self.shujuimagvie.image = [UIImage imageNamed:@"details_dealbg"];
    [self  addSubview: self.shujuimagvie];
    self.zhawushujuLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 124+33+9, kScreenW, 12)];
    self.zhawushujuLabel.text = @"暂无开服";
    self.zhawushujuLabel.font = [UIFont systemFontOfSize:12];
    self.zhawushujuLabel.textAlignment = NSTextAlignmentCenter;
    self.zhawushujuLabel.textColor = RGB(102, 102, 102);
    [self  addSubview:self.zhawushujuLabel];
    
    
    self.maintablview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW,  kScreenH-NavBarHeight-235-40-TabbarSafeBottomMargin-60) style:UITableViewStylePlain];
    self.maintablview.backgroundColor = [UIColor whiteColor];
    self.maintablview.delegate = self;
    self.maintablview.dataSource = self;
    self.maintablview.tag = 10;
  //  self.maintablview.scrollEnabled = NO;
    self.maintablview.separatorStyle =  UITableViewCellSeparatorStyleNone;
    self.maintablview.tableFooterView = [[UIView alloc]init];
    self.maintablview.showsVerticalScrollIndicator = NO;
    [self addSubview:self.maintablview];
    [self addrefreshControl];
    
    
    
}

- (void)addrefreshControl {
    
//    self.maintablview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
//    [self.maintablview.mj_header beginRefreshing];
    self.maintablview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
    
    
}

// 下拉加载
- (void)underRefresh {
    
    
       self.p = @"1";
      NSDictionary *parametes  = nil;
      if ([[HWLocallyStoredData getLoginUserID] isEqualToString:@""]) {
          
          parametes = @{
                                                    @"id" : self.gameId,
                                                    @"p" : self.p,
                                                    @"ios" : @"1",
                                                    @"ct" :  @"app2021",
                                                    @"ac" : @"game_info2"
                                                    };
          
          
      } else {
                parametes = @{
                                              @"id" : self.gameId,
                                              @"p" : self.p,
                                              @"ios" : @"1",
                                              @"ct" : @"app2021",
                                              @"ac" : @"game_info2"
                                              };
          
          
          
          
      }
         
       
    self.shujuMuArr = [[NSMutableArray alloc]init];
    
        
      [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
            
           [self.maintablview.mj_header endRefreshing];
           [self.maintablview.mj_footer endRefreshing];
          
          
            
            NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
            
            if ([codeStr intValue] == 1) {
                NSLog(@"%@",response);
               
               
                 NSMutableArray *arr   =  response[@"data"];
               
                    
                   //  self.shujuimagvie.hidden = YES ;
                    self.maintablview.hidden = NO;
                    
              
                
                
                
                
                for (int i = 0 ; i < arr.count; i++) {
                    [self.shujuMuArr addObject:arr[i]];

                }
                
                
                self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                [self.maintablview reloadData];
                
                

            } else {
                
                self.maintablview.hidden = YES;
                [self.maintablview.mj_header endRefreshing];
                [self.maintablview.mj_footer endRefreshing];
        
                [MBProgressHUD showError:response[@"message"]];
                
            }
         
            
            
        } failure:^(NSString *error) {
          
            [self.maintablview.mj_header endRefreshing];
            [self.maintablview.mj_footer endRefreshing];
        }];
      
    
    
    
    
    
    
}

//上拉加载
- (void)onRefresh {
    
    
        NSDictionary  *parametes = @{
                                @"id" : self.gameId,
                                @"p" : self.p,
                                @"ios" : @"1",
                                @"ct" : @"app2021",
                                @"ac" : @"game_info2"
                             };
    
    
     int p = [self.p intValue];
                   
     int max_p = [self.max_p intValue];
                   
      if (p > max_p) {
          
          [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
                       
            return ;
                       
        }
    
    
      [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
            
           [self.maintablview.mj_header endRefreshing];
           [self.maintablview.mj_footer endRefreshing];
          
          
            
            NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
            
            if ([codeStr intValue] == 1) {
                NSLog(@"%@",response);
               
               
                 NSMutableArray *arr   =  response[@"data"];
                
                for (int i = 0 ; i < arr.count; i++) {
                    [self.shujuMuArr addObject:arr[i]];

                }
            
                self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                [self.maintablview reloadData];
                
        
          
            } else {
                
                
                [self.maintablview.mj_header endRefreshing];
                [self.maintablview.mj_footer endRefreshing];
                
                
                [MBProgressHUD showError:response[@"message"]];
                
            }
         
            
            
        } failure:^(NSString *error) {
          
            [self.maintablview.mj_header endRefreshing];
            [self.maintablview.mj_footer endRefreshing];
        }];
      
    
    
    
    
}




#pragma  mark -------  添加tanviewhederView
- (void)addTabviewhHderView {
    
    UIView *hederView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, 36)];
    UILabel *namelabel = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, kScreenW-30, 36)];
    namelabel.text = @"仅供参考，以游戏内实际开服为准";
    namelabel.font = [UIFont systemFontOfSize:13];
    namelabel.textColor = RGB(51, 51, 51);
    [hederView addSubview:namelabel];

    self.maintablview.tableHeaderView = hederView;
    
    
}





- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return  1;
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  
    return  self.shujuMuArr.count;
    
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


        static NSString *cellID = @"celiD";
        HWOpenClothingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell = [[HWOpenClothingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        }
        cell.selectionStyle =  UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor whiteColor];
        NSDictionary *dic = self.shujuMuArr[indexPath.row];
        cell.dataDic = dic;
    
    
        return  cell;
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return  30;
    
    
}










- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    self.scrollCallback(scrollView);
    
}


#pragma mark - JXPagingViewListViewDelegate

- (UIScrollView *)listScrollView {
    return self.maintablview;
}

- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
    self.scrollCallback = callback;
}

- (UIView *)listView {
    return self;
}



@end
